package com.luoqiz.file.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.jodconverter.DocumentConverter;
import org.jodconverter.document.DefaultDocumentFormatRegistry;
import org.jodconverter.office.OfficeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FileConverterUtil {
	@Autowired
	private DocumentConverter documentConverter;

	public void docToPDF(File inputFile, File outputFile) throws IOException, OfficeException {
		documentConverter.convert(inputFile).to(outputFile).execute();

	}

	public void docToPDF(String inputFilePath, String outputFilePath) throws IOException, OfficeException {

		File inputFile = new File(inputFilePath);
		File outputFile = new File(outputFilePath);

		documentConverter.convert(inputFile).to(outputFile).execute();
		// Stop the office process
	}

	public void docToPDF(FileInputStream fileInputStream, FileOutputStream fileOutputStream)
			throws IOException, OfficeException {
		documentConverter.convert(fileInputStream, true).as(DefaultDocumentFormatRegistry.DOC)
				.to(fileOutputStream, true).as(DefaultDocumentFormatRegistry.PDF).execute();
	}

	public void docxToPDF(FileInputStream fileInputStream, FileOutputStream fileOutputStream)
			throws IOException, OfficeException {
		documentConverter.convert(fileInputStream, true).as(DefaultDocumentFormatRegistry.DOCX)
				.to(fileOutputStream, true).as(DefaultDocumentFormatRegistry.PDF).execute();
	}

	public void htmlToDocx(InputStream fileInputStream, OutputStream fileOutputStream)
			throws IOException, OfficeException {
		documentConverter.convert(fileInputStream, true).as(DefaultDocumentFormatRegistry.HTML)
				.to(fileOutputStream, true).as(DefaultDocumentFormatRegistry.DOCX).execute();
	}

}
